package com.suse.lhw.security.service.Impl;

import com.suse.lhw.constan.const_phr_status;
import com.suse.lhw.dao.PHealthDataMapper;
import com.suse.lhw.dao.PIfoMapper;
import com.suse.lhw.domin.PHealthData;
import com.suse.lhw.tools.CaloriesCalculate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.suse.lhw.dao.PHealthrecordMapper;
import com.suse.lhw.domin.PHealthrecord;
import com.suse.lhw.security.service.PHealthrecordService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.security.service.Impl
 * @ClassName: PHealthrecordServiceImpl
 * @Description: java类作用描述
 * @Author: 李鸿炜
 * @CreateDate: 2020/4/15 10:30
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/4/15 10:30
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service
@Transactional

public class PHealthrecordServiceImpl implements PHealthrecordService {

    @Resource
    private PHealthrecordMapper pHealthrecordMapper;
    @Resource
    private PHealthDataMapper pHealthDataMapper;
    @Resource
    private PIfoMapper pIfoMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return pHealthrecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(PHealthrecord record) {
        return pHealthrecordMapper.insert(record);
    }

    @Override
    public int insertSelective(PHealthrecord record) {
        return pHealthrecordMapper.insertSelective(record);
    }

    @Override
    public PHealthrecord selectByPrimaryKey(Integer id) {
        return pHealthrecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(PHealthrecord record) {
        return pHealthrecordMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(PHealthrecord record) {
        return pHealthrecordMapper.updateByPrimaryKey(record);
    }

    @Override
    public PHealthrecord selectAHealthRecord(String userid, int totalFoodCalories, int totalMovementCalories) {
        /**
         * @Author:Hongwei Li
         * @Description:查找一个健康记录，通过今天的日期，用户的id查找，如果没有找到
         *               则重新建立一个，每次新建的时候，根据用户的健康数据，算出今天
         *               需要的热量差。如果用户的健康数据更新，会更改健康记录的status
         *               查找时用户的记录也应该更改，
         *
         * @Date:17:21 2020/5/21
         * @Param:[]
         * @Return:com.suse.lhw.domin.PHealthrecord
         **/
        PHealthrecord pHealthrecord = pHealthrecordMapper.selectByUseridAndDate(userid, new Date());
        PHealthData pData = pHealthDataMapper.selectByUserid(userid);

        if (pHealthrecord == null) {
            //建立新的健康记录

            pHealthrecord = new PHealthrecord(userid, pData.getStarttime(), pData.getNewWeight(), pData.getEndtime()
                    , pData.getTargetWeight(), pData.getNewWeight(), const_phr_status.updated, new Date());
            pHealthrecord.setAteCalories(totalFoodCalories);
            pHealthrecord.setMovementCalories(totalMovementCalories);
            int dailyCosumedCalories = CaloriesCalculate.dailyBasicCosumed(pData, pIfoMapper.selectByUserId(userid));
            int eat_calories = CaloriesCalculate.couldEat(dailyCosumedCalories, pData, totalMovementCalories, totalFoodCalories);
            pHealthrecord.setAteCalories(totalFoodCalories);
            pHealthrecord.setMovementCalories(totalMovementCalories);
            pHealthrecord.setEatCalories(eat_calories);
            pHealthrecord.setDailyCosumedCalories(dailyCosumedCalories);
            return pHealthrecord;
        } else {
            pHealthrecord.setAteCalories(totalFoodCalories);
            pHealthrecord.setMovementCalories(totalMovementCalories);
            //先看是否需要更新
            if (pHealthrecord.getStatus() == const_phr_status.needupdate) {
                pHealthrecord = new PHealthrecord(userid, pData.getStarttime(), pData.getNewWeight(), pData.getEndtime()
                        , pData.getTargetWeight(), pData.getNewWeight(), const_phr_status.updated, new Date());
                pHealthrecordMapper.updateByPrimaryKeySelective(pHealthrecord);

            }
            //无论需要更新，则应该算一下卡路里
            int dailyCosumedCalories = CaloriesCalculate.dailyBasicCosumed(pData, pIfoMapper.selectByUserId(userid));
            int eat_calories = CaloriesCalculate.couldEat(dailyCosumedCalories, pData, totalMovementCalories, totalFoodCalories);
            int regular = CaloriesCalculate.getRegular(dailyCosumedCalories, pData, totalMovementCalories, totalFoodCalories);
            pHealthrecord.setRegular(regular);
            pHealthrecord.setEatCalories(eat_calories);
            pHealthrecord.setDailyCosumedCalories(dailyCosumedCalories);
            return pHealthrecord;
        }
    }

    @Override
    public boolean isHRecordExit(String userId, Date date) {
        PHealthrecord pHealthrecord = pHealthrecordMapper.selectByUseridAndDate(userId, new Date());
        if (pHealthrecord == null) {
            //建立新的健康记录
            return false;
        }
        return true;
    }

}



